package servlet;

import bean.Result;
import biz.BizException;
import biz.EmployeeBiz;
import com.google.gson.Gson;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "SaveEmployeeServlet", value = "/SaveEmployeeServlet.s")
public class SaveEmployeeServlet extends HttpServlet {
    private EmployeeBiz biz = new EmployeeBiz();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Result result;

        try {
            String name = request.getParameter("name");
            String position = request.getParameter("position");
            String entryTime = request.getParameter("entryTime");
            String department = request.getParameter("department");
            String salary = request.getParameter("salary");
            String telphone = request.getParameter("telphone");
            String id = request.getParameter("id");
            System.out.println(name);
            System.out.println(position);
            System.out.println(salary);
            if (id != null){
                System.out.println(position);
                biz.modify(id, name, position, entryTime, salary, department, telphone);
            }else{
                biz.create(name, position, entryTime, salary, department, telphone);
            }
            result = new Result(1, "成员添加成功!", null);
        } catch (BizException e) {
            e.printStackTrace();
            result = new Result(0, e.getMessage(), null);
        }
        String json = new Gson().toJson(result);
        response.getWriter().append(json);

    }
}
